const baseUrl = 'https://watererp.kuashou.com';
const globalData = getApp().globalData;
const wxP = (wxFun, obj = {}) => new Promise(
  (resolve, reject) => {
    const promiseParam = {
      success: resolve,
      fail: reject,
    }
    const objChange = Object.assign({}, obj, promiseParam);
    return wxFun(objChange);
  }
);
const request = obj => new Promise(
  (resolve, reject) => {
    const promiseParam = {
      url: baseUrl + obj.url,
      success: res => resolve(res.data),
      fail: err => reject(err.errMsg),
      header: {
        token: globalData.token,
      }
    };
    const objChange = Object.assign({}, obj, promiseParam);
    return wx.request(objChange);
  }
);
const reqPost = obj => request(Object.assign({}, obj, { method: 'POST' }));
const uploadFile = obj => {
  const objChange = {
    url: baseUrl + '/upload/upload_image',
    name: 'file',
    header: {
      token: globalData.token,
    }
  }
  return wxP(wx.uploadFile, Object.assign({}, objChange, obj)).then(res => JSON.parse(res.data));
};
const getPrevPage = (numPrev = 1) => {
  const pages = getCurrentPages();
  return pages[pages.length - numPrev - 1]
};
export default {
  wxP,
  request,
  reqPost,
  uploadFile,
  getPrevPage
};
